package day8.zy1;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

public class Market {
    public void show(){
        SAXReader reader=new SAXReader();
        String path="D:\\kgcjava\\javaadv\\src\\day8\\products.xml";

        Document doc=null;
        try {
            doc = reader.read(new File(path));
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        System.out.println("根节点"+doc.getRootElement().getName());
        List<Element> list = doc.selectNodes("/shop/product");
        for (Element e :list){
            Element eitemno = (Element) e.selectSingleNode("itemno");
            int itemno = Integer.parseInt(eitemno.getText());
            Element ename = (Element) e.selectSingleNode("name");
            String name = ename.getTextTrim();
            Element eprice = (Element) e.selectSingleNode("price");
            Double price = Double.parseDouble(eprice.getText());
            Element estockcount = (Element) e.selectSingleNode("stockcount");
            int stockcount = Integer.parseInt(estockcount.getText());
            Element eunit = (Element) e.selectSingleNode("unit");
            String unit = eunit.getTextTrim();
            System.out.printf("商品编号:%d\t商品名称:%s\t商品单价:%f\t库存量:%d\t计价单位:%s\n",
                    itemno, name, price, stockcount, unit);
        }
    }
    public void chazhao(int itemno) {
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(new File("D:\\kgcjava\\javaadv\\src\\day8\\products.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        List<Element> list = doc.selectNodes("/shop/product[itemno='" + itemno + "']");
        if (list.size() == 0) {
            System.out.println("无商品");
            return;
        }
        for (Element e : list) {
            Element eitemno = (Element) e.selectSingleNode("itemno");
            int sitemno = Integer.parseInt(eitemno.getText());
            Element ename = (Element) e.selectSingleNode("name");
            String name = ename.getTextTrim();
            Element eprice = (Element) e.selectSingleNode("price");
            Double price = Double.parseDouble(eprice.getText());
            Element estockcount = (Element) e.selectSingleNode("stockcount");
            int stockcount = Integer.parseInt(estockcount.getText());
            Element eunit = (Element) e.selectSingleNode("unit");
            String unit = eunit.getTextTrim();
            System.out.printf("商品编号:%d\t商品名称:%s\t商品单价:%f\t库存量:%d\t计价单位:%s\n",
                    sitemno, name, price, stockcount, unit);
        }
    }
    public  void xiugai(int itemno, int stock) {
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(new File("D:\\kgcjava\\javaadv\\src\\day8\\products.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Element node = (Element) doc.selectSingleNode("/shop/product[itemno='" + itemno + "']");
        if (node == null) {
            System.out.println("无商品");
            return;
        }
        node.selectSingleNode("stockcount").setText(String.valueOf(stock));
        try {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");
            XMLWriter xw = new XMLWriter(new FileOutputStream("D:\\kgcjava\\javaadv\\src\\day8\\products.xml"), format);
            xw.write(doc);
            xw.close();
            System.out.println("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void goumai(int itemno, int shuliang) {
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(new File("D:\\kgcjava\\javaadv\\src\\day8\\products.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        List<Element> list = doc.selectNodes("/shop/product[itemno='" + itemno + "']");
        if (list.size() == 0) {
            System.out.println("无商品");
            return;
        }
        for (Element e : list) {
            Element estockcount = (Element) e.selectSingleNode("stockcount");
            int stockcount = Integer.parseInt(estockcount.getText());

            Element eprice = (Element) e.selectSingleNode("price");
            Double price = Double.parseDouble(eprice.getText());

            boolean product = Boolean.valueOf(e.attributeValue("discount"));

            if (shuliang > stockcount || shuliang < 0) {
                System.out.println("商品不存在或输入错误");
            } else {
                e.selectSingleNode("stockcount").setText(String.valueOf(stockcount - shuliang));
                try {
                    OutputFormat format = OutputFormat.createPrettyPrint();
                    format.setEncoding("utf-8");
                    XMLWriter xw = new XMLWriter(new FileOutputStream("D:\\kgcjava\\javaadv\\src\\day8\\products.xml"), format);
                    xw.write(doc);
                    xw.close();
                } catch (Exception o) {
                    o.printStackTrace();
                }
                if (product) {
                    double dz = price * shuliang * 0.7;
                    System.out.println("应付款：" + dz + "元");
                } else {
                    double zj= price * shuliang;
                    System.out.println("应付款：" + zj + "元");
                }
            }
        }
    }
    public void tianjia(boolean discount, String factoryaddr, int itemno, String name, double price, int stockcount, String unit) {
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(new File("D:\\kgcjava\\javaadv\\src\\day8\\products.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        List<Element> list = doc.selectNodes("/shop/product[itemno='" + itemno + "'][name='" + name + "']");
        if (list.size() == 0) {

            Element root = doc.getRootElement();
            Element newSp = root.addElement("product");

            newSp.addAttribute("discount", String.valueOf(discount));
            newSp.addAttribute("factoryaddr", factoryaddr);

            Element eitemno = newSp.addElement("itemno");
            eitemno.setText(String.valueOf(itemno));

            Element eprice = newSp.addElement("price");
            eprice.setText(String.valueOf(price));

            Element estockcount = newSp.addElement("stockcount");
            estockcount.setText(String.valueOf(stockcount));

            Element eunit = newSp.addElement("unit");
            eunit.setText(unit);
            try {
                OutputFormat format = OutputFormat.createPrettyPrint();
                format.setEncoding("utf-8");
                XMLWriter xw = new XMLWriter(new FileOutputStream("D:\\kgcjava\\j    avaadv\\src\\day8\\products.xml"), format);
                xw.write(doc);
                xw.close();
            } catch (Exception o) {
                o.printStackTrace();
            }
        } else {
            System.out.println("商品已存在，不可添加");
        }


    }
}
